package com.ruoyi.web.controller.customer;

import java.util.Date;
import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.customer.domain.BusCustomerComplaints;
import com.ruoyi.customer.service.IBusCustomerComplaintsService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户投诉Controller
 *
 * @author xiaochen
 * @date 2022-11-30
 */
@RestController
@RequestMapping("/complaints/complaints")
public class BusCustomerComplaintsController extends BaseController
{
    @Autowired
    private IBusCustomerComplaintsService busCustomerComplaintsService;

    /**
     * 查询客户投诉列表
     */
    @PreAuthorize("@ss.hasPermi('complaints:complaints:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusCustomerComplaints busCustomerComplaints)
    {
        startPage();
        List<BusCustomerComplaints> list = busCustomerComplaintsService.selectBusCustomerComplaintsList(busCustomerComplaints);
        return getDataTable(list);
    }

    /**
     * 导出客户投诉列表
     */
    @PreAuthorize("@ss.hasPermi('complaints:complaints:export')")
    @Log(title = "客户投诉", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusCustomerComplaints busCustomerComplaints)
    {
        List<BusCustomerComplaints> list = busCustomerComplaintsService.selectBusCustomerComplaintsList(busCustomerComplaints);
        ExcelUtil<BusCustomerComplaints> util = new ExcelUtil<BusCustomerComplaints>(BusCustomerComplaints.class);
        util.exportExcel(response, list, "客户投诉数据");
    }

    /**
     * 获取客户投诉详细信息
     */
    @PreAuthorize("@ss.hasPermi('complaints:complaints:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(busCustomerComplaintsService.getById(id));
    }

    /**
     * 新增客户投诉
     */
    @PreAuthorize("@ss.hasPermi('complaints:complaints:add')")
    @Log(title = "客户投诉", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusCustomerComplaints busCustomerComplaints)
    {
        return toAjax(busCustomerComplaintsService.save(busCustomerComplaints));
    }

    /**
     * 修改客户投诉
     */
    @PreAuthorize("@ss.hasPermi('complaints:complaints:edit')")
    @Log(title = "客户投诉", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusCustomerComplaints busCustomerComplaints)
    {
        BusCustomerComplaints newObject = busCustomerComplaintsService.getById(busCustomerComplaints.getId());
        BeanUtils.copyProperties(busCustomerComplaints, newObject);
        newObject.setUpdatedTime(new Date());
        newObject.setUpdatorId(SecurityUtils.getUserId());
        return toAjax(busCustomerComplaintsService.updateById(newObject));
    }

    /**
     * 删除客户投诉
     */
    @PreAuthorize("@ss.hasPermi('complaints:complaints:remove')")
    @Log(title = "客户投诉", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(busCustomerComplaintsService.removeBatchByIds(Arrays.asList(ids)));
    }

    /**
     * 处理投诉
     */
    @PreAuthorize("@ss.hasPermi('complaints:complaints:audit')")
    @Log(title = "处理投诉", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody BusCustomerComplaints busCustomerComplaints)
    {
        if (busCustomerComplaints.getStatus() == 0) {
            throw new ServiceException("必选选择处理方式");
        }
        BusCustomerComplaints old = busCustomerComplaintsService.getById(busCustomerComplaints.getId());

        if(old.getStatus().equals(busCustomerComplaints.getStatus())) {
            throw new ServiceException("非法操作");
        }
        BusCustomerComplaints newObject = busCustomerComplaintsService.getById(busCustomerComplaints.getId());
        BeanUtils.copyProperties(busCustomerComplaints, newObject);
        newObject.setUpdatedTime(new Date());
        newObject.setHandleTime(new Date());
        newObject.setUpdatorId(SecurityUtils.getUserId());
        return toAjax(busCustomerComplaintsService.updateById(newObject));
    }

}
